package com.c.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.c.entity.Information;
import com.c.entity.Permission;
import com.c.entity.Role;
import com.c.entity.User;
import com.c.mapper.InformationMapper;
import com.c.mapper.PermissionMapper;
import com.c.mapper.RoleMapper;
import com.c.mapper.UserMapper;
import com.c.service.InformationService;
import com.c.service.UserService;
import com.c.utils.DateUtils;
import com.c.utils.PageResult;
import com.c.utils.QueryPageBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author wangxiangli
 * @create 2022-12-25-12:30
 */
@Service
@Transactional
public class InformationServiceImpl extends ServiceImpl<InformationMapper, Information> implements InformationService {

    /**
     * 资讯分页查询
     * @param queryPageBean
     * @return
     */
    @Override
    public PageResult pageQuery(QueryPageBean queryPageBean) {
        IPage<Information> pageInformation=new Page<>(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());
        QueryWrapper<Information> wrapper=new QueryWrapper();
        String queryString = queryPageBean.getQueryString();
        //判断条件值是否为空，如果不为空拼接条件
        if (StringUtils.hasText(queryString)) {
            queryString = queryPageBean.getQueryString().trim();// 去除查询条件首尾空格
            wrapper.like("title",queryString);
            wrapper.orderByDesc("id");
        }
        IPage page1 = baseMapper.selectPage(pageInformation, wrapper);
        return new PageResult(page1.getTotal(),page1.getRecords());

    }

    /**
     * 新增资讯信息
     * @param information
     * @throws Exception
     */
    @Override
    public void addInformation(Information information) throws Exception {
        String date=new SimpleDateFormat("yyyy-MM-dd").format(new Date());
        java.sql.Date date1 = java.sql.Date.valueOf(date);
        information.setCratetime(date1);
        information.setUpdatetime( date1);
        baseMapper.insert(information);

    }

    /**
     * 编辑资讯信息
     * @param information
     * @throws Exception
     */
    @Override
    public void editInformation(Information information) throws Exception {
        String date=new SimpleDateFormat("yyyy-MM-dd").format(new Date());
        java.sql.Date date1 = java.sql.Date.valueOf(date);
        information.setUpdatetime(date1);
        baseMapper.updateById(information);
    }

}
